﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Core
{
	public static class Utilities
	{
		public static string GetBaseNameSpace(string modelNameSpace)
		{
			return modelNameSpace.Remove(modelNameSpace.IndexOf(".Model"));
		}

		/// <summary>
		/// for the sake of being consistent.
		/// </summary>
		/// <param name="modelNameSpace"></param>
		/// <returns></returns>
		public static string GetModelNameSpace(string modelNameSpace)
		{
			return GetBaseNameSpace(modelNameSpace) + ".Model";
		}

		public static string GetSupportNameSpace(string modelNameSpace)
		{
			return GetBaseNameSpace(modelNameSpace) + ".Support";
		}

		public static string GetSupportRequestNameSpace(string modelNameSpace)
		{
			return GetSupportNameSpace(modelNameSpace) + ".Request";
		}

		public static string GetLogicNameSpace(string modelNameSpace)
		{
			return GetBaseNameSpace(modelNameSpace) + ".Logic";
		}

		public static string GetDaoNameSpace(string modelNameSpace)
		{
			return GetModelNameSpace(modelNameSpace) + ".Dao";
		}

		public static string GetFilterNameSpace(string modelNameSpace)
		{
			return GetDaoNameSpace(modelNameSpace);
		}

		public static string GetDaoSupportNameSpace(string modelNameSpace)
		{
			return GetDaoNameSpace(modelNameSpace) + ".Support";
		}

		public static Type GetRealType(Type t)
		{
			Type toReturn = t;
			if (t.Name.StartsWith("Nullable")) {
				toReturn = Nullable.GetUnderlyingType(t);
			}

			return toReturn;
		}

		public static Type GetRealTypeFromMemberProperty(MemberInfo member)
		{
			return GetRealType(((PropertyInfo)member).PropertyType);
		}

		public static bool IsPrimitive(Type type)
		{
            return type == typeof (int) || type == typeof (long) ||
			       type == typeof (Guid) || type == typeof (string) ||
			       type == typeof (DateTime) || type == typeof (byte) ||
			       type == typeof (char) || type == typeof (float) ||
				   type == typeof(double) || type == typeof(bool) || type == typeof(decimal);
		}


		public static string GetCommonNameForType(string type)
		{
			string retValue;

			switch (type) {
				case "Int32":
					retValue = "int";
					break;
				case "Int64":
					retValue = "long";
					break;
				case "Single":
					retValue = "float";
					break;
				case "Double":
					retValue = "double";
					break;
				case "Boolean":
					retValue = "bool";
					break;
				default:
					retValue = type;
					break;
			}

			return retValue;
		}

		public static string Capitalize(
			string str)
		{
			string retStr = null;

			if (str != null)
			{
				retStr = str[0].ToString().ToUpper() + str.Substring(1);
			}

			return retStr;
		}
	}
}
